<template>
  <div id="pictureDetailPage">
    <a-row :gutter="[16, 16]">
      <!-- 图片预览 -->
      <a-col :sm="24" :md="16" :xl="18">
        <a-card title="图片预览">
          <a-image :src="picture.url" style="max-height: 600px; object-fit: contain"/>
        </a-card>
      </a-col>
      <!-- 图片信息 -->
      <a-col :sm="24" :md="8" :xl="6">
        <a-card title="图片信息">
          <a-descriptions :column="1">
            <a-descriptions-item label="作者">
              <a-space :size="24" :src="picture.user?.userAvatar"/>
              <div>{{ picture.user?.userName }}</div>
            </a-descriptions-item>
            <a-descriptions-item label="名称">
              {{ picture.name ?? '未命名'}}
            </a-descriptions-item>
            <a-descriptions-item label="简介">
              {{ picture.introduction ?? '无'}}
            </a-descriptions-item>
            <a-descriptions-item label="分类">
              {{ picture.category ?? '默认'}}
            </a-descriptions-item>
            <a-descriptions-item label="标签">
              <a-tag v-for="tag in picture.tags" :key="tag">
                {{ tag }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="格式">
              {{ picture.picFormat ?? '-'}}
            </a-descriptions-item>
            <a-descriptions-item label="宽度">
              {{ picture.picWidth ?? '-'}}
            </a-descriptions-item>
            <a-descriptions-item label="高度">
              {{ picture.picHeight ?? '-'}}
            </a-descriptions-item>
            <a-descriptions-item label="宽高比">
              {{ picture.picScale ?? '-'}}
            </a-descriptions-item>
            <a-descriptions-item label="大小">
              {{ formatSize(picture.picSize) }}
            </a-descriptions-item>
          </a-descriptions>
          <!-- 操作 -->
          <a-space wrap>
            <a-button type="primary" @click="doDownload">
              下载
              <template #icon>
                <DownloadOutlined/>
              </template>
            </a-button>
            <a-button type="primary" ghost @click="doShare">
              分享
              <ShareModal ref="shareModalRef" :link="shareLink" title="分享"/>
              <template #icon>
                <share-alt-outlined/>
              </template>
            </a-button>
            <a-button v-if="canEdit" :icon="h(EditOutlined)" type="default" @click="doEdit">
              编辑
            </a-button>
            <a-button v-if="canEdit" :icon="h(DeleteOutlined)" danger @click="doDelete">
              删除
            </a-button>
          </a-space>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { computed, h, onMounted, ref } from 'vue'
import { EditOutlined, DeleteOutlined, ShareAltOutlined, DownloadOutlined } from '@ant-design/icons-vue'
import { downloadImage, formatSize} from '@/utils'
import { useLoginUserStore } from '@/stores/user.ts'
import { deletePictureUsingPost, getPictureVoByIdUsingGet } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import ShareModal from '@/components/picture/ShareModal.vue'

interface Props {
  id: string | number | undefined
}

const props = defineProps<Props>()
const picture = ref<API.PictureVO>({})
const loginUserStore = useLoginUserStore()
const router = useRouter()
const route = useRoute()
const shareModalRef = ref()
const shareLink = ref<string>()

const canEdit = computed(() => {
  const loginUser = loginUserStore.loginUser
  // 登录可编辑
  if (!loginUser.id) {
    return false
  }
  // 仅本人或管理员可编辑
  const user = picture.value.user || {}
  return loginUser.id === user.id || loginUser.userRole == 'admin';
})

const fetchPictureDetail = async () => {
  try {
    const res = await getPictureVoByIdUsingGet({
      id: route.params.id,
    })
    console.log('res', res)
    if (res.data.code === 0 && res.data.data) {
      picture.value = res.data.data
    } else {
      message.error('获取图片详情失败,' + res.data.message)
    }
  } catch (e: any) {
    message.error('获取图片详情失败,' + e.message)
  }
}

const doEdit = () => {
  router.push('/add_picture?id=' + picture.value.id)
}

const doDelete = async () => {
  const id = picture.value.id
  if (!id) return
  const res = await deletePictureUsingPost({ id })
  if (res.data.code === 0) {
    message.success('删除成功')
  } else {
    message.error('删除失败,' + res.data.message)
  }
}

const doDownload = () => {
  downloadImage(picture.value.url)
}

const doShare = (e: Event) => {
  e.stopPropagation()
  console.log(window.location)
  shareLink.value = `${window.location.protocol}//${window.location.host}${window.location.pathname}`
    shareModalRef.value.openModal()
}
onMounted(() => {
  fetchPictureDetail()
})
</script>

<style scoped>
#pictureDetailPage {
  margin-bottom: 16px;
}
</style>
